/*
 * Copyright 2007 - The JDPF Project Team (http://www.jdpf.org) 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.jdpf.core.kernel.elements;

import org.jdpf.core.kernel.logging.ILogger;
import org.jdpf.core.kernel.logging.LoggerFactory;

public class InspectorSubNet {
	
	private static final ILogger logger = LoggerFactory.getDefault().getLogger();
	private static final String INPUT_ARC_PREFIX = "inputarc";
	private static final String OUTPUT_ARC_PREFIX = "outputarc";
	
    private Place inputPlace, outputPlace;

    private InputArc inputArc;
    private OutputArc outputArc; // TODO remove local variable
	private ITransition inspector;

    private static int count = 0;

    public InspectorSubNet(ITransition inspector) {
		this.inspector = inspector;
		
		AbstractTransition abTr = null;
		try {
			abTr = (AbstractTransition) inspector.getClass().getMethod(
					"getAbstractTransition", (Class[])null).invoke(inspector, (Object[])null);
		} catch (Exception e) {
			logger.error(this, e.getMessage());
		}
    	String transId=abTr.getId();
		
		String idInputArc = InspectorSubNet.INPUT_ARC_PREFIX + count++ + transId;
		String idOutputArc = InspectorSubNet.OUTPUT_ARC_PREFIX + count++ + transId;
	
		inputPlace = new Place();
		outputPlace = new Place();
		inputArc = new InputArc(idInputArc, inputPlace, inspector);
		inputArc.setExpression(new ArcExpression());
		outputArc = new OutputArc(idOutputArc, inspector, outputPlace);
    }

    public void addToken(Token token) {
    	inputPlace.addToken(token);
    }
    
    public ITransition getInspectorTransition(){
    	return inspector;
    }
}
